package helperClasses;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: Jaan Sepp and Martti Elias
 * Date: 20.09.11
 * Time: 21:00
 * To change this template use File | Settings | File Templates.
 */
public class FileHelper {
  public String readFromFile(String fileName) {
    String fileContents = "";
    String temp;
    try {
      FileReader fileReader = new FileReader("./files/" + fileName);
      BufferedReader br = new BufferedReader(fileReader);
      while((temp = br.readLine()) != null) {
        fileContents += temp + "\n";
      }
      br.close();
      System.out.println("Successfully read from file: " + fileName + ".");
    } catch (FileNotFoundException e) {
      System.out.println("File " + fileName + " not found");
    } catch (IOException e) {
      System.out.println("Unable to read from file: " + fileName + ".");
    }
    return fileContents;
  }

  public void writeToFile(String fileName, String contents) {
    try {
      FileWriter fileWriter = new FileWriter("./files/" + fileName);
      BufferedWriter bw = new BufferedWriter(fileWriter);
      bw.write(contents);
      bw.flush();
      bw.close();
      System.out.println("Successfully created file: " + fileName + ".");
    } catch (IOException e) {
      System.out.println("Unable to create file: " + fileName + ".");
    }
  }
}
